package com.ng.desktop.expansionsa.view.cliente;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;

import com.ng.desktop.expansionsa.model.Cliente;
import com.ng.desktop.expansionsa.model.TipoCliente;
import com.ng.desktop.expansionsa.persistence.ClientePersistence;
import com.ng.desktop.expansionsa.services.NGFactoryBean;
import com.ng.framework.persistence.NGSearchParameter;
import com.ng.framework.persistence.NGSearchParameter.Type;
import com.ng.framework.persistence.NGSearchParameter.UnaryOperation;
import com.ng.framework.view.desktop.NGABMCFrame;
import com.ng.framework.view.desktop.NGGUIUtils;
import com.ng.framework.view.desktop.NGTablePanel;
import com.ng.framework.view.desktop.filters.NGFilter;
import com.ng.framework.view.desktop.filters.NGTextFieldFilter;

public class ClienteFrame extends NGABMCFrame<Cliente, ClientePersistence> {
	
	private static final long serialVersionUID = -2693436144810647895L;

	private NGFilter tipoClienteFilter;
	private NGFilter nombreFilter;
	private NGFilter apellidoFilter;
	private NGFilter dniFilter;
	private NGFilter razonSocialFilter;
	private NGFilter cuitFilter;
	
	public ClienteFrame() {
		this.setTitle("Gestión de Clientes");
		this.setFrameIcon(NGGUIUtils.getImageIcon("icons/user-worker.png"));
		this.setSize(new Dimension(985, 600));
		this.centrar();
	}

	@Override
	protected ClientePersistence createPersistence() {
		return (ClientePersistence) NGFactoryBean.getInstance().getBean("clientePersistence");
	}

	@Override
	protected List<NGFilter> createFilters() {
		List<NGFilter> filters = new ArrayList<NGFilter>();
		
		filters.add(this.getTipoClienteFilter());
		filters.add(this.getRazonSocialFilter());
		filters.add(this.getCuitFilter());
		filters.add(this.getNombreFilter());
		filters.add(this.getApellidoFilter());
		filters.add(this.getDniFilter());
		
		return filters;
	}
	
	private NGFilter getTipoClienteFilter() {
		if(tipoClienteFilter == null) {
			tipoClienteFilter = new TipoClienteFilter();
		}
		
		return tipoClienteFilter;
	}
	
	private NGFilter getNombreFilter() {
		if(nombreFilter == null) {
			nombreFilter = new NGTextFieldFilter("Nombre", Cliente.class, "nombre", Type.STRING, UnaryOperation.LIKE);
			nombreFilter.setEnabled(false);
		}
		
		return nombreFilter;
	}
	
	private NGFilter getApellidoFilter() {
		if(apellidoFilter == null) {
			apellidoFilter = new NGTextFieldFilter("Apellido", Cliente.class, "apellido", Type.STRING, UnaryOperation.LIKE);
			apellidoFilter.setEnabled(false);
		}
		
		return apellidoFilter;
	}
	
	private NGFilter getDniFilter() {
		if(dniFilter == null) {
			dniFilter = new NGTextFieldFilter("DNI", Cliente.class, "dni", Type.STRING, UnaryOperation.EQUALS);
			dniFilter.setEnabled(false);
		}
		
		return dniFilter;
	}
	
	private NGFilter getRazonSocialFilter() {
		if(razonSocialFilter == null) {
			razonSocialFilter = new NGTextFieldFilter("Razón Social", Cliente.class, "razonSocial", Type.STRING, UnaryOperation.LIKE);
		}
		
		return razonSocialFilter;
	}
	
	private NGFilter getCuitFilter() {
		if(cuitFilter == null) {
			cuitFilter = new NGTextFieldFilter("CUIT", Cliente.class, "cuit", Type.STRING, UnaryOperation.EQUALS);
		}
		
		return cuitFilter;
	}

	@Override
	protected NGTablePanel<Cliente, ClientePersistence> createTable(
			ClientePersistence persistence) {
		return new ClienteTablePanel(persistence);
	}
	
	private class TipoClienteFilter extends NGFilter implements ActionListener {

		private static final long serialVersionUID = 6362606487272901301L;
		
		private JRadioButton particularRadioButton;
		private JRadioButton empresaRadioButton;
		
		public TipoClienteFilter() {
			super("Tipo de Cliente");
			
			this.buildGui();

			ButtonGroup buttonGroup = new ButtonGroup();
			buttonGroup.add(this.getEmpresaRadioButton());
			buttonGroup.add(this.getParticularRadioButton());			
		}
		
		private void buildGui() {
			this.setLayout(new GridBagLayout());
			
			this.add(this.getEmpresaRadioButton(), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
			this.add(this.getParticularRadioButton(), new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		}
		
		private JRadioButton getParticularRadioButton() {
			if(particularRadioButton == null) {
				particularRadioButton = new JRadioButton("Particular");
				particularRadioButton.addActionListener(this);
			}
			
			return particularRadioButton;
		}
		
		private JRadioButton getEmpresaRadioButton() {
			if(empresaRadioButton == null) {
				empresaRadioButton = new JRadioButton("Empresa", true);
				empresaRadioButton.addActionListener(this);
			}
			
			return empresaRadioButton;
		}

		@Override
		public List<NGSearchParameter> getSearchParameters() {
			return Arrays.asList(new NGSearchParameter[] { new NGSearchParameter(Cliente.class, "tipoCliente", this.getTipoClienteSeleccionado().name(), Type.STRING, UnaryOperation.EQUALS) });
		}
		
		private TipoCliente getTipoClienteSeleccionado() {
			return this.getEmpresaRadioButton().isSelected() ? TipoCliente.EMPRESA : TipoCliente.PARTICULAR;
		}

		@Override
		public void clear() {}

		@Override
		public void actionPerformed(ActionEvent e) {
			boolean empresa = this.getTipoClienteSeleccionado().equals(
					TipoCliente.EMPRESA);

			ClienteFrame.this.getRazonSocialFilter().setEnabled(empresa);
			ClienteFrame.this.getCuitFilter().setEnabled(empresa);

			ClienteFrame.this.getNombreFilter().setEnabled(!empresa);
			ClienteFrame.this.getApellidoFilter().setEnabled(!empresa);
			ClienteFrame.this.getDniFilter().setEnabled(!empresa);

			if (empresa) {
				ClienteFrame.this.getNombreFilter().clear();
				ClienteFrame.this.getApellidoFilter().clear();
				ClienteFrame.this.getDniFilter().clear();
			} else {
				ClienteFrame.this.getRazonSocialFilter().clear();
				ClienteFrame.this.getCuitFilter().clear();
			}			
		}
		
	}

}
